package com.diko.educms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diko.educms.entity.EduBanner;
import com.diko.educms.mapper.EduBannerMapper;
import com.diko.educms.service.EduBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author Emma-Li
 * @since 2020-10-13
 */
@Service
public class EduBannerServiceImpl extends ServiceImpl<EduBannerMapper, EduBanner> implements EduBannerService {

    @Override
    public void pageBanner(Page<EduBanner> pageParam, Object o) {
        baseMapper.selectPage(pageParam,null);
    }

    @Override
    public EduBanner getBannerById(String id) {
        return baseMapper.selectById(id);
    }

    @CacheEvict(value = "banner",allEntries = true)
    @Override
    public void saveBanner(EduBanner banner) {
        baseMapper.insert(banner);
    }

    @CacheEvict(value = "banner",allEntries = true)
    @Override
    public void updateBannerById(EduBanner banner) {
        baseMapper.updateById(banner);
    }

    @CacheEvict(value = "banner",allEntries = true)
    @Override
    public void removeBannerById(String id) {
        baseMapper.deleteById(id);
    }

    @Cacheable(value = "banner",key = "'selectIndexList'")
    @Override
    public List<EduBanner> selectIndexList() {
        QueryWrapper<EduBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        //last方法，拼接sql语句
        wrapper.last("limit 2");
        List<EduBanner> list = baseMapper.selectList(null);
        return list;
    }
}
